package com.pagepub.core.ftp;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Iterator;
import java.util.List;

import com.pagepub.core.utils.Config;
import com.pagepub.core.utils.ExtendString;
import com.pagepub.expo.entity.CorpPublication;
import com.pagepub.expo.entity.CorpPublicationPage;


public class UploadXml {

	public static final String FILE_SEPARATOR = System.getProperties()
			.getProperty("file.separator");

	public String UploadXml(CorpPublication publication, List publist, String result) {
		String realFileName = "false";
		realFileName = CreateXml(publication, publist, result);
		return realFileName;
	}

	public String ModUploadXml(CorpPublication publication, List publist,
			String result) {
		
		String realFileName = "false";
		realFileName =this.UploadXml(publication, publist, result);
		return realFileName;
	}

	public String CreateXml(CorpPublication publication, List publist, String result) {
		String resultdstr = "false";
		String repeat = "true";
		String canzoom = "true";
		String filePath = "";
		String thumbpathname = "";
		String ipagepathname="";
		String endpathname="";
		String fileExt = "";
		String ipagefileExt="";
		String endpathfileExt="";
		long backgroundalpha=100;
		OutputStreamWriter pw = null;
		FileOutputStream fos = null;
		try {
			if (publication != null) {
				try {
					// String result =
					// getServlet().getServletContext().getRealPath(FILE_SEPARATOR);
					// String realFileName =
					// publication.getMemberid()+publication.getPublicationid()+FormatDateTime.getToday("yyyyMMddHHmmss")
					// + "config.xml";
					String realFileName = "config.xml";
					filePath = result + FILE_SEPARATOR+  publication.getPubPath() + FILE_SEPARATOR;// 取当前系统路径电子书
					// 保存创建xml文件
					boolean exists = (new File(filePath)).exists();
					if (!exists) {
						(new File(filePath)).mkdirs();
					}
					fos = new FileOutputStream(filePath + FILE_SEPARATOR
							+ realFileName);// 建立一个文件的输出流
					if (publication.getBackgroundRepeat() == 1) {
						repeat = "false";
						canzoom = "False";
					}

					pw = new OutputStreamWriter(fos, "utf-8");
					// PrintWriter pw=new PrintWriter(fout);
					pw.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
					pw.write("<qz version=\"1.0\">");
					pw.write("<Editor>" + publication.getCorpId()
							+ "</Editor>");

					pw.write("<title>"
							+ ExtendString.nullToSpaceNoTrim(publication
									.getTitle()) + "</title>");
					if (publication.getPubType() == 1) {
						pw.write("<width>"
								+ (publication.getWidth() * 45 / 100) * 2
								+ "</width>");
					} else {
						pw.write("<width>"
								+ (publication.getWidth() * 45 / 100)*2
								+ "</width>");
					}
					pw
						.write("<height>" + publication.getHeight() * 45 / 100
								+ "</height>");
//					if(publication.getBackgroundalpha()==0){
//						backgroundalpha=100;
//					}
//					if (publication.getBackground() != null
//							&& !"".equals(publication.getBackground())) {
//						pw.write("<bg alpha=\""
//								+ backgroundalpha
//								+ "\" repeat=\""
//								+ repeat
//								+ "\">"
//								+ ExtendString.nullToSpaceNoTrim(publication
//										.getBackground()) + "</bg>");
//					} else {
//						pw.write("<bg alpha=\""
//								+ backgroundalpha
//								+ "\" repeat=\"" + repeat + "\">adPlayer.swf</bg>");
//					}
					if (publication.getBackground() != null
							&& !"".equals(publication.getBackground())) {
						pw.write("<bgSound  loop=\"10\">"
								+ publication.getBackgroundSound()
								+ "</bgSound>");
					} else {
						pw.write("<bgSound  loop=\"10\">" +  Config.ImageServerURL_CORPPUBIMG+"default_bgsound.mp3</bgSound>");
					}

					pw.write("<body scroll=\"False\">");
					
					if (publication.getThumbPath() != null) {
						thumbpathname = publication.getThumbPath();
						fileExt = thumbpathname.substring(thumbpathname
								.length() - 3, thumbpathname.length());
					}
					if (!"swf".equals(fileExt.toLowerCase())) {
						pw.write("<page canzoom=\"" + canzoom + "\">");
						pw.write("<url>ipages/" + publication.getThumbPath()
								+ "</url>");
						pw.write("  <thumb>thumb/small_"
								+ publication.getThumbPath() + "</thumb>");
						pw.write("</page>");

//						if (publication.getPubtype() == 2) {
//							pw.write("<page canzoom=\"" + canzoom + "\">");
//							pw.write("<url>ipages/"
//									+ publication.getThumbpath() + "</url>");
//							pw.write("  <thumb>thumb/small_"
//									+ publication.getThumbpath() + "</thumb>");
//							pw.write("</page>");
//						}

					} else {

						//pw.write("<url></url>");
						pw.write("<page canzoom=\"False\">");
						pw.write("<thumb>ipages/"+ publication.getThumbPath() + "</thumb>");
						pw.write("</page>");

//						if (publication.getPubtype() == 2) {
//							pw.write("<page canzoom=\"false\">");
//							pw.write("  <thumb>ipages/"
//									+ publication.getThumbpath() + "</thumb>");
//							pw.write("</page>");
//						}
					}

					if (publist != null) {

						for (Iterator ir = publist.iterator(); ir.hasNext();) {
							CorpPublicationPage publicationpage = (CorpPublicationPage) ir
									.next();
							if (publicationpage.getIpagePath() != null) {
								ipagepathname = publicationpage.getIpagePath();
								ipagefileExt = ipagepathname.substring(ipagepathname
										.length() - 3, ipagepathname.length());
							}
							if (!"swf".equals(ipagefileExt.toLowerCase())) {
								pw.write("<page canzoom=\"" + canzoom + "\">");
								pw
										.write("<url>ipages/"
												+ publicationpage.getIpagePath()
												+ "</url>");
								pw.write("  <thumb>thumb/"
										+ publicationpage.getThumbPath()
										+ "</thumb>");
								pw.write("</page>");	
							}else{
								pw.write("<page canzoom=\"false\">");
								pw.write("  <thumb>ipages/"
										+ publicationpage.getThumbPath()
										+ "</thumb>");
								pw.write("</page>");
							}
							
							if (publication.getPubType() == 2) {
								if (!"swf".equals(ipagefileExt.toLowerCase())) {
									pw.write("<page canzoom=\"" + canzoom + "\">");
									pw.write("<url>ipages/"
											+ publicationpage.getIpagePath()
											+ "</url>");
									pw.write("  <thumb>thumb/"
											+ publicationpage.getThumbPath()
											+ "</thumb>");
									pw.write("</page>");	
								}else{
									pw.write("<page canzoom=\"false\">");
									pw.write("  <thumb>ipages/"
											+ publicationpage.getThumbPath()
											+ "</thumb>");
									pw.write("</page>");
								}
								
							}
						}
					}
					
					if (publication.getEndPath() != null) {
						endpathname = publication.getEndPath();
						endpathfileExt = endpathname.substring(endpathname
								.length() - 3, endpathname.length());
					}
					if (!"swf".equals(endpathfileExt.toLowerCase())) {
						pw.write("<page canzoom=\"" + canzoom + "\">");
						pw.write("<url>ipages/" + publication.getEndPath()
								+ "</url>");
						pw.write("  <thumb>thumb/small_" + publication.getEndPath()
								+ "</thumb>");
						pw.write("</page>");	
					}else{
						pw.write("<page canzoom=\"false\">");			
						pw.write("  <thumb>ipages/" + publication.getEndPath()
								+ "</thumb>");
						pw.write("</page>");
					}
					
//					if (publication.getPubtype() == 2) {
//						if (!"swf".equals(endpathfileExt.toLowerCase())) {
//							pw.write("<page canzoom=\"" + canzoom + "\">");
//							pw.write("<url>ipages/" + publication.getEndpath()
//									+ "</url>");
//							pw.write("  <thumb>thumb/small_"
//									+ publication.getEndpath() + "</thumb>");
//							pw.write("</page>");	
//						}else{
//							pw.write("<page canzoom=\"false\">");					
//							pw.write("  <thumb>ipages/"
//									+ publication.getEndpath() + "</thumb>");
//							pw.write("</page>");
//						}
//						
//					}
					pw.write("</body>");
					pw.write("</qz>");
					resultdstr = realFileName;
				} catch (Exception E) {
					System.out.println("电子书配置创建失败!" + E.getMessage());
				} finally {
					pw.flush();
				}
			}

		} catch (Exception E) {
			System.out.println("字典创建失败!" + E.getMessage());
			return resultdstr;
		} finally {

			if (pw != null) {
				try {
					pw.flush();
					pw.close();
					fos.close();
				} catch (IOException e) {
					System.err.print(e);
				}
			}
		}
		return resultdstr;
	}

}